<template>
    <view class="data-box">
        <view class="data-fixed-box">
            <!-- 搜索 -->
            <search
                @searchText="searchText"
                @openObjTypeList="openObjTypeList"
                @searchAction="searchAction"
                @inputSearchKw="inputSearchKw"
                @inputFocus="inputFocus"
                @setTitleValue="setTitleValue"
                :placeholder="placeholder"
                :total="total"
                :md="mdDj"
                ref="search"
            >
            </search>
            <!-- 单据类型 -->
            <changeBills
                ref="changeBills"
                @setST="setST"
                @setFilters="setFilters"
                v-show="!isSearching"
            ></changeBills>
            <!-- 过滤弹出 -->
            <filters
                v-show="!isSearching"
                @setST="setST"
                @setHandlers="setHandlers"
                @setShopid="setShopid"
                @setTime="setTime"
                @setYunShopId="setYunShopId"
                :filltersData="filltersData"
                :orderData="orderData"
                ref="filter"
            >
            </filters>
            <searchTotal :total="total" v-show="isSearching"></searchTotal>
            <searchPop
                ref="searchPop"
                :type="2"
                @changeObjType="changeObjType"
                @showAndHideTypeList="showAndHideTypeList"
                @setDefaultObjType="setDefaultObjType"
            >
            </searchPop>
            <searchKwResult
                ref="searchKwResult"
                @doSearch="doSearch"
            ></searchKwResult>
        </view>
        <!-- 单据列表 -->
        <billsList
            @setBillsInfoData="setBillsInfoData"
            @orderList="orderList"
            @searchKwBillList="searchKwBillList"
            ref="setBillsId"
            :isSearching="isSearching"
            :billsLs="billsLs"
            :hOffset="hOffset"
            :st="orderData.st"
        >
        </billsList>
    </view>
</template>

<script>
import search from "../comm/Search.vue";
import changeBills from "./ChangeBills.vue";
import filters from "./filters.vue";
import billsList from "./BillsList.vue";
import staticConst from "../../js/const/StaticConst.js";
import config from "../../js/conf/Config.js";

import eventCtrl from "../../js/event/EventCtrl.js";

import searcher from "../../js/search/Searcher.js";

import searchPop from "../comm/SearchPop.vue";

import https from "../../js/https/Https.js";

import searchKwResult from "../comm/SearchKwResult.vue";

import searchTotal from "../comm/SearchTotal.vue";

import accessCtrl from "../../js/limit/AccessCtrl.js";

import localStore from "../../js/LocalStore.js";

let pageNo = 0;
let pageSize = staticConst.PAGE_SIZE;
export default {
    components: {
        search,
        changeBills,
        filters,
        billsList,
        searchPop,
        searchKwResult,
        searchTotal,
    },
    data() {
        return {
            orderData: {
                // ch : 1,//渠道可不传
                st: 13, //单据状态
                kw: "", //搜索关键词
                shopid: "", //门店ID
                shopid_yd: "", //云店id
                uid_cp: "", //经手人ID
                time: "", //时间类型
                billType: 1, //
            },
            billsLs: [],
            filltersData: {
                ydBillsState: "全部",
                ydListState: "全部云店",
                billsState: "已确认",
                timeState: "全部时间",
                storesState: "全部门店",
                handlersState: "全部经手人",
                ydListStateIdex: "",
                ydBillStateIndex: 13,
                statisticalIndex: 2,
                TimeStateIndex: "",
                StoresStateIndex: "",
                HandlersStateIndex: "",
            },
            placeholder: "搜索单据",
            isLoading: false,
            curPage: 1, //记录点击单据类型筛选的值
            isSearching: false,
            hOffset: 150,

            objType: null,
            keyWord: "",
            total: 0,
            exactKw: null,
            mdDj: staticConst.YY_DJ,
            mySelf: null,
        };
    },

    methods: {
        async initData() {
            this.mySelf = await localStore.getUserInfo();
            this.clearPageLs();
            this.orderList();
            searcher.loadBillSchObjTypeLs(searcher.CH_YYO);
        },
        pageDefaultTab(tab) {
            this.$refs.changeBills.pageDefaultTab(tab);
        },
        /*****搜索单据****/
        doSearch(item) {
            this.clearPageLs();
            let data = {};
            data.exact = 1;
            this.$refs.search.setCatSearchTxt(item.title);
            data.kw = item.title;
            this.exactKw = data;
            this.searchKwBillList();
        },

        /***设置输入框，选择的搜索关键字值****/
        setTitleValue(item) {
            this.$refs.search.setCatSearchTxt(item.title);
        },

        /****搜索单据****/
        searchKwBillList() {
            if (!this.exactKw) return;
            if (this.isLoading) return;
            this.isLoading = true;
            let that = this;
            let obj = {};
            if (this.objType) obj.objtype = this.objType.code;
            obj.md = staticConst.YY_ADD_GOODS;
            obj.exact = this.exactKw.exact;
            obj.kw = this.exactKw.kw;
            obj.pfrom = pageNo * pageSize;
            obj.pnum = pageSize;
            https.searchKwBillList(obj, callBack);
            function callBack(data) {
                that.isLoading = false;
                if (data && data.status == 1) {
                    if (!data.data) return;
                    if (data.data.total) that.total = data.data.total;
                    if (!data.data.ls) return;
                    that.billsLs = that.billsLs.concat(data.data.ls);
                    if (pageNo == 0) {
                        if (that.billsLs.length > 0) {
                            let orderData = {};
                            orderData.type = that.billsLs[0].type;
                            orderData.odid = that.billsLs[0].id;
                            that.$refs.setBillsId.setBillsId(
                                that.billsLs[0].id
                            );
                            that.$emit("setBillsThree", orderData);
                        }
                    }
                    pageNo++;
                }
            }
        },

        /***输入关键字搜索*****/
        inputSearchKw(kw) {
            let data = {};
            let that = this;
            this.keyWord = kw;
            data.kw = kw;
            if (this.objType) data.objtype = this.objType.code;
            data.ch = searcher.CH_YYO;

            https.searchKwByObjType(data, callBack);
            function callBack(obj) {
                if (obj.status == 1) {
                    that.$refs.searchKwResult.setKeyWord(that.keyWord);
                    if (obj.data && obj.data.ls && obj.data.ls.length) {
                        that.$refs.searchKwResult.setListData(obj.data.ls);
                    } else {
                        that.$refs.searchKwResult.setListData([]);
                    }
                } else {
                    that.$refs.searchKwResult.setListData([]);
                }
            }
        },

        /*****输入框获取焦点处理****/
        inputFocus() {
            this.$refs.searchPop.init();
            if (this.objType) this.placeholder = "请输入" + this.objType.name;
            this.$refs.search.searchAction(true);
        },

        /***弹出搜索类型列表***/
        openObjTypeList(value) {
            this.searchAction(true);
            if (value) {
                this.$refs.searchPop.show("show");
                if (this.objType)
                    this.placeholder = "请输入" + this.objType.name;
            } else {
                this.$refs.searchPop.show();
            }
        },

        /****初始化设置默认objType*****/
        setDefaultObjType(item) {
            this.objType = item;
        },

        /***改变搜索类型***/
        changeObjType(item) {
            this.placeholder = "请输入" + item.name;
            this.objType = item;
            this.$refs.searchPop.show();
        },

        showAndHideTypeList(value) {
            this.$refs.search.setIsOpenList(value);
        },

        /*******/
        searchAction(value) {
            this.isSearching = value;
            this.clearPageLs();
            if (!value) {
                this.$refs.searchPop.show();
                this.exactKw = null;
                this.keyWord = "";
                (this.total = 0), (this.placeholder = "搜索单据");
                this.$refs.search.clearSearchTxt();
                this.$refs.searchKwResult.setListData([]);
                this.$refs.searchKwResult.setListData("");
                this.clearPageLs();
                this.orderList();
                this.hOffset = 150;
            } else {
                this.hOffset = 110;
            }
        },

        /*** 搜索 ***/
        searchText(kw) {
            /*****获取选择的关键字****/
            let item = this.getSelectedKwTitle();
            this.$refs.searchKwResult.setShowValue(false);
            this.clearPageLs();
            let data = {};
            if (item != null) {
                this.keyWord = item.title;
                data.exact = 1;
                this.$refs.search.setCatSearchTxt(item.title);
            } else {
                this.keyWord = kw;
                data.exact = 2;
            }
            data.kw = this.keyWord;
            this.exactKw = data;
            this.searchKwBillList();
        },

        /*****获取选择的关键字****/
        getSelectedKwTitle() {
            let item = this.$refs.searchKwResult.getSelectedItem();
            return item;
        },

        /****清除数据***/
        clearPageLs() {
            pageNo = 0;
            this.billsLs = [];
        },

        /*** 检测是否展示过滤栏 ***/
        setFilters(index) {
            console.log("index====================:", index);
            this.clearPageLs();
            this.orderData = {
                st: 13, //单据状态
                kw: "", //搜索关键词
                shopid: "", //门店ID
                shopid_yd: "", //云店id
                uid_cp: "", //经手人ID
                time: "", //时间类型
                billType: index, //
            };
            this.hOffset = 150;
            this.curPage = index;
            this.$refs.filter.resetFilters();
            this.orderData.st = index == 1 ? 13 : index;
            this.orderList();
            this.filltersData = {
                ydBillsState: "全部",
                ydListState: "全部云店",
                billsState: "已确认",
                timeState: "全部时间",
                storesState: "全部门店",
                handlersState: "全部经手人",
                ydListStateIdex: "",
                ydBillStateIndex: 13,
                statisticalIndex: 2,
                TimeStateIndex: "",
                StoresStateIndex: "",
                HandlersStateIndex: "",
            };
            uni.$emit(eventCtrl.ORDER_LIST_EVENT, index);
        },

        setBillsInfoData(res) {
            let orderData = {};
            orderData.type = res.type;
            orderData.odid = res.id;
            this.$emit("setBillsThree", orderData);
            this.$refs.setBillsId.setBillsId(res.id);
        },

        /*** 设置订单筛选 ***/
        setST(res) {
            console.log(res, "res==================>");
            this.clearPageLs();
            this.orderData.st = res;
            this.orderList();
        },

        /*** 设置时间删选 ***/
        setTime(res) {
            this.clearPageLs();
            this.orderData.time = res;
            this.orderList();
        },

        /*** 设置经手人筛选 ***/
        setHandlers(res) {
            this.clearPageLs();
            this.orderData.uid_cp = res;
            this.orderList();
        },

        /*** 设置门店筛选 ***/
        setShopid(res) {
            this.clearPageLs();
            this.orderData.shopid = res;
            this.orderList();
        },

        /****设置云店id*****/
        setYunShopId(res) {
            this.clearPageLs();
            this.orderData.shopid_yd = res;
            this.orderList();
        },

        /*** 云单/销售单据列表  ORDER_LS ***/
        async orderList(index) {
            if (this.isLoading) return;
            this.isLoading = true;
            let url = config.YY0_ORDER_ORDER_LS;
            let param = {};
            let access = await accessCtrl.getViewOtherSaleLimit();
            if (!access) {
                //没有查看他人单据权限，只能每次填上自己的id
                this.orderData.uid_cp = this.mySelf.uid;
            }
            param = this.orderData;
            if (this.orderData.billType == 1) {
                param.ch = 2;
            }

            param.pfrom = pageNo * pageSize;
            param.pnum = pageSize;

            let res = await this.$api({ url: url, data: param });
            this.isLoading = false;
            if (!res || !res.data) {
                if (pageNo == 0) {
                    this.$emit("setBillsThree");
                }
                return;
            }
            if (index == "ss") {
                this.billsLs = res.data;
            } else {
                this.billsLs = this.billsLs.concat(res.data);
            }
            if (this.billsLs.length > 0 && pageNo == 0) {
                let orderData = {};
                orderData.type = this.billsLs[0].type;
                orderData.odid = this.billsLs[0].id;
                if (index == 1) orderData.yd = 1;
                this.$refs.setBillsId.setBillsId(this.billsLs[0].id);
                this.$emit("setBillsThree", orderData);
            }
            pageNo++;
        },

        /***注册时间***/
        addEvent() {
            let that = this;
            uni.$on(eventCtrl.ACTION_ORDER_EVENT, function (data) {
                that.actionHandler(data);
            });
            uni.$on(eventCtrl.PAY_IN_AND_OUT_EVENT, function (data) {
                that.payInAndOut(data);
            });
        },

        /****清除事件****/
        removeEvent() {
            uni.$off(eventCtrl.ACTION_ORDER_EVENT);
            uni.$off(eventCtrl.PAY_IN_AND_OUT_EVENT);
        },

        /***收款/退款更新***/
        payInAndOut(data) {
            this.removeEvent();
            this.addEvent();
            let i,
                len = this.billsLs.length;
            for (i = 0; i < len; i++) {
                let obj = this.billsLs[i];
                if (obj.id == data.id) {
                    // data.type == 1 --->  退款/收款    data.type == 2 ---> 退货申请
                    if (data.type == 1) {
                        obj.status_act.status_pay_txt = data.status_pay_txt;
                        obj.amount_notpay = data.amount_notpay;
                    } else {
                        obj.status_act.status_rt_txt = "退货申请已驳回";
                    }

                    console.log("ok");
                    // this.billsLs[i] = obj;
                    break;
                }
            }
        },

        /****处理操作菜单***/
        actionHandler(data) {

            this.removeEvent();
            this.addEvent();
            this.clearPageLs();
            this.orderList();
            return
            let i,
                len = this.billsLs.length;
            for (i = 0; i < len; i++) {
                let obj = this.billsLs[i];
                if (obj.id == data) {
                    this.billsLs.splice(i, 1);
                    break;
                }
            }
            len = this.billsLs.length;
            if (len > 0) {
                let index = 0;
                if (len > 1) {
                    if (i > 1) {
                        index = i - 1;
                    } else {
                        index = i;
                    }
                }
                let obj = this.billsLs[index];
                if (obj) {
                    this.setBillsInfoData(obj);
                }
            }
        },

        /****修改云单价格*****/
        changeYunBillPrice(data) {
            // let len = this.billsLs.length;
            // console.log('changeYunBillPricechangeYunBillPricechangeYunBillPrice')
            // for (let i = 0; i < len; i++) {
            //     let temp = this.billsLs[i];
            //     if (temp.id == data.id) {
            //         temp.amount_all = data.amount_all;
            //         temp.amount_notpay = data.amount_notpay;
            //         break;
            //     }
            // }
            this.clearPageLs();
            this.orderList();
        },

        onClickPageHandler(event) {
            let detail = event.detail;
            if (detail.x < 72 || detail.x > 437 || detail.y < 70) {
                this.$refs.filter.onClickPageHandler(event);
            }
        },
    },

    mounted() {
        this.addEvent();
    },
};
</script>
<style lang="scss">
.data-box {
    position: fixed;
    margin-left: 0;
    box-sizing: border-box;
    width: 345px;
    height: 100vh;
    border-right: 1px solid #ececec;
    .data-fixed-box {
        width: 345px;
        box-sizing: border-box;
        border-right: 1px solid #ececec;
        position: fixed;
        top: 0;
        background: #fff;
        z-index: 1;
    }
}
</style>